package com.xj.center.data.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ie.api.center.data.fegin.RealDataClient;
import com.ie.api.center.manager.feign.DeviceClient;
import com.ie.api.center.manager.feign.TenantClient;
import com.xj.center.data.service.PointValueService;
import com.xj.common.bean.Pages;
import com.xj.common.bean.R;
import com.xj.common.bean.constant.Common;
import com.xj.common.bean.point.PointValue;
import com.xj.common.dto.PointValueDto;
import com.xj.common.model.Device;
import com.xj.common.model.Tenant;
import com.xj.common.utils.JsonPaseUtil;
import com.xj.common.utils.KafkaProducer;
import com.xj.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 实时数据接口
 * @author wsp
 *
 */
@RestController
@RequestMapping(value = Common.Service.IE_DATA_REAL)
public class RealDataAPI implements RealDataClient {

    @Resource
    private PointValueService pointValueService;

    /**
     * 通过设备id获取该设备的所有实时数据
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = Common.Service.IE_DATA_REAL_BY_DEVICEID,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    @Override
    public R<List<PointValue>> handleGetRealDataByDeviceid(
            @RequestBody JSONObject jsonParam)
    {
        Long deviceId = jsonParam.getLong("deviceId");
        Boolean history = jsonParam.getBoolean("history");
        try {
            List<PointValue> pointValues = pointValueService.realtime(deviceId);
            if (null == pointValues) {
                pointValues = pointValueService.latest(deviceId);
            }
            if (null != pointValues) {
                if (history) {
                    pointValues.forEach(pointValue -> {
                        if (!pointValue.getType().equals(Common.ValueType.STRING)) {
                            PointValueDto pointValueDto = (new PointValueDto()).setDeviceId(deviceId).setPointId(pointValue.getPointId()).setPage((new Pages()).setSize(100L));
                            Page<PointValue> page = pointValueService.list(pointValueDto);
                            if (null != page) {
                                pointValue.setChildren(page.getRecords().stream()
                                        .map(pointValueChild -> pointValueChild.setId(null).setDeviceId(null).setPointId(null)).collect(Collectors.toList()));
                            }
                        }
                    });
                }
                return R.ok(pointValues);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();

    }
    //通过设备id和电号，获取实时数据
    @RequestMapping(value = Common.Service.IE_DATA_REAL_BY_DEVICEID_POINTID,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public R<PointValue> handleGetRealDataByDeviceIdAndPointID(
            @RequestBody JSONObject jsonParam
    )
    {
        Long deviceId = jsonParam.getLong("deviceId");
        Long pointId = jsonParam.getLong("pointId");
        Boolean history = jsonParam.getBoolean("history");
        try {
            PointValue pointValue = pointValueService.realtime(deviceId, pointId);
            if (null == pointValue) {
                pointValue = pointValueService.latest(deviceId, pointId);
            }
            if (null != pointValue) {
                if (history) {
                    PointValueDto pointValueDto = (new PointValueDto()).setDeviceId(deviceId).setPointId(pointId).setPage((new Pages()).setSize(100L));
                    Page<PointValue> page = pointValueService.list(pointValueDto);
                    if (null != page) {
                        pointValue.setChildren(page.getRecords().stream()
                                .map(pointValueChild -> pointValueChild.setId(null).setDeviceId(null).setPointId(null)).collect(Collectors.toList()));
                    }
                }
                return R.ok(pointValue);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();

    }

    /**
     * 通过复杂条件PointValueDto，过滤历史数据
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = Common.Service.IE_DATA_LIST,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public R<Page<PointValue>> handleGetPointValueList(
            @RequestBody JSONObject jsonParam
    )
    {
        PointValueDto pointValueDto = JsonPaseUtil.JSONStrToObj(jsonParam.toJSONString(), PointValueDto.class);
        try {
            Page<PointValue> page = pointValueService.list(pointValueDto);
            if (null != page) {
                return R.ok(page);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();

    }

}
